    package com.smart.home.config;


    import com.smart.home.interceptor.HomeContextClearingFilter;
    import com.smart.home.interceptor.JwtTokenAdminInterceptor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;

/**
 * 配置类，注册web层相关组件
 */
//TODO 这里功能要搞懂
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

        @Autowired
        private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

        @Autowired
        private HomeContextClearingFilter homeContextClearingFilter;




        //TODO 看看这个什么时候清理，可能有问题
        @Bean
        public FilterRegistrationBean<HomeContextClearingFilter> homeContextClearingFilterRegistration() {
            FilterRegistrationBean<HomeContextClearingFilter> registration = new FilterRegistrationBean<>();
            registration.setFilter(homeContextClearingFilter);
            registration.addUrlPatterns("/*"); // 应用于所有请求路径
            registration.setOrder(1); // 设置过滤器顺序（越小越靠前）
            return registration;
        }

        /**
         * 注册自定义拦截器
         * @param registry
         */
        protected void addInterceptors(InterceptorRegistry registry) {
            log.info("开始注册自定义拦截器...");



            registry.addInterceptor(jwtTokenAdminInterceptor)
                    .addPathPatterns("/user/**")
                    .addPathPatterns("/home/**")
                    .addPathPatterns("/homeMember/**")
                    .addPathPatterns("/devices/**")
                    .addPathPatterns("/devicesType/**")
                    .excludePathPatterns("/user/login");
        }




    /**
     * 通过knife4j生成用户管理模板接口文档
         * @return
         */
        @Bean
        public Docket docket1() {
            ApiInfo apiInfo = new ApiInfoBuilder()
                    .title("智能家居控制系统接口文档")
                    .version("2.0")
                    .description("智能家居控制系统接口文档")
                    .build();
            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .groupName("用户端")
                    .apiInfo(apiInfo)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.smart.home.controller.User"))
                    .paths(PathSelectors.any())
                    .build();
            return docket;
        }

        @Bean
        public Docket docket2() {
            ApiInfo apiInfo = new ApiInfoBuilder()
                    .title("智能家居控制系统接口文档")
                    .version("2.0")
                    .description("智能家居控制系统接口文档")
                    .build();
            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .groupName("家庭管理")
                    .apiInfo(apiInfo)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.smart.home.controller.Home"))//到包就行，不用到类
                    .paths(PathSelectors.any())
                    .build();
            return docket;
        }

    @Bean
    public Docket docket3() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("智能家居控制系统接口文档")
                .version("2.0")
                .description("智能家居控制系统接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("设备管理")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.smart.home.controller.Devices"))//到包就行，不用到类
                .paths(PathSelectors.any())
                .build();
        return docket;
    }





        /**
         * 设置静态资源映射
         * @param registry
         */
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
